//
//  CS_Development_EditFocusView.m
//  Pods
//
//  Created by 网新中研 on 2017/5/8.
//
//

#import "CS_Development_EditFocusView.h"

@interface CS_Development_EditFocusView()<UIAlertViewDelegate>

@property (nonatomic, strong) UIButton *deleteButton;

@property (nonatomic, strong) UIButton *addButton;

@property (nonatomic, strong) UIButton *editButton;

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) CSPageElementModel *pageElementModel;

@end

@implementation CS_Development_EditFocusView

static CS_Development_EditFocusView *_editFocusView;

/**
 * 单例对象
 */
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _editFocusView = [CS_Development_EditFocusView new];
        STYLE_DEVELOPMENT(_editFocusView);
        _editFocusView.layer.borderColor = [UIColor colorFromHexCode:@"a1c8df"].CGColor;
//        _editFocusView.layer.borderWidth = 0.7;
//        _editFocusView.layer.cornerRadius = 5;
//        _editFocusView.layer.masksToBounds = YES;
//        _editFocusView.backgroundColor = [UIColor colorFromHexCode:@"cdebfe"];
        _editFocusView.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
        _editFocusView.layer.opacity = 0.9;
        _editFocusView.layer.borderColor = COLOR_THEME.CGColor;
        _editFocusView.layer.borderWidth = 0.3;
    });
    return _editFocusView;
}

#define BUTTON_WIDTH  50.0
#define BUTTON_MARGIN 20.0

/**
 * 绑定视图
 */
-(void)bindView:(CSPageElementModel *)targetModel{
    self.pageElementModel = targetModel;
    if (self.pageElementModel.viewInstance) {
        [self.backgroundView removeFromSuperview];
        
        //想界面添加视图，并使之等大
        self.frame = CGRectMake(5, 0, self.pageElementModel.viewInstance.frame.size.width - 10, self.pageElementModel.viewInstance.frame.size.height);
        [self.pageElementModel.viewInstance addSubview:self];
        
        self.backgroundView.frame = self.frame;
        [self.pageElementModel.viewInstance insertSubview:self.backgroundView atIndex:0];
        
        //添加删除按钮
        self.deleteButton.frame = CGRectMake(self.frame.size.width/2.0 + 0.5 * BUTTON_WIDTH + BUTTON_MARGIN, (self.frame.size.height - BUTTON_WIDTH/2.0) / 2.0, BUTTON_WIDTH, BUTTON_WIDTH / 2.0);
        [self addSubview:self.deleteButton];
        
        //添加添加按钮
        self.addButton.frame = CGRectMake(self.frame.size.width/2.0 - 0.5 * BUTTON_WIDTH, (self.frame.size.height - BUTTON_WIDTH / 2.0) / 2.0, BUTTON_WIDTH, BUTTON_WIDTH / 2.0);
        [self addSubview:self.addButton];
        
        //添加添加按钮
        self.editButton.frame = CGRectMake(self.frame.size.width/ 2.0 - 1.5 * BUTTON_WIDTH - BUTTON_MARGIN, (self.frame.size.height - BUTTON_WIDTH / 2.0) / 2.0, BUTTON_WIDTH, BUTTON_WIDTH / 2.0);
        [self addSubview:self.editButton];
        
    }
}

#pragma mark --- click handler

-(void)deleteClick{

    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定删除该元素吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
    
}

-(void)addClick{
    
    CSSelectableModel *model1 = [CSSelectableModel new];
    model1.code = @"1231";
    model1.name = @"标题(CS_Page_SectionTitleView)";
    
    CSSelectableModel *model2 = [CSSelectableModel new];
    model2.code = @"1231";
    model2.name = @"属性(CS_Page_TextFieldView)";
    
    CSSelectableModel *model3 = [CSSelectableModel new];
    model3.code = @"1231";
    model3.name = @"图片(CS_Page_ImageView)";
    
    WS(weakSelf);
    CS_Pop_SingleSelectView *pageSingleSelectView = [CS_Pop_SingleSelectView pop_SingleSelectViewForModel:@"视图类型" selectItems:@[model1, model2, model3] selectHandler:^(NSInteger section, NSInteger index, CSSelectableModel *model) {
        
        switch (index) {
            case 0:
            {
                CSPageElementModel *sectionTitle = [CSPageElementModel new];
                sectionTitle.type = CSViewType_PageSectionTitleView;
                sectionTitle.title = [NSString stringWithFormat:@"标题栏(测试)"];
                [weakSelf.pageElementModel.pageEngine.pageModel.elements insertObject:sectionTitle atIndex:[weakSelf getElementIndex]];
                sectionTitle.pageEngine = weakSelf.pageElementModel.pageEngine;
                [weakSelf.pageElementModel.pageEngine refreshPage];
            }
                break;
            case 1:
            {
                CSPageElementModel *pageText = [CSPageElementModel new];
                pageText.type = CSViewType_PageTextView;
                pageText.title = [NSString stringWithFormat:@"属性(测试)"];
                pageText.defaultValue = @"是大幅度发";
                [weakSelf.pageElementModel.pageEngine.pageModel.elements insertObject:pageText atIndex:[weakSelf getElementIndex]];
                pageText.pageEngine = weakSelf.pageElementModel.pageEngine;
                [weakSelf.pageElementModel.pageEngine refreshPage];
            }
                break;
                
            default:
                break;
        }
        
    }];
    
    [pageSingleSelectView popShow];

}

-(void)editClick{
    
}

-(NSInteger)getElementIndex{
    
    return [self.pageElementModel.pageEngine.pageModel.elements indexOfObject:self.pageElementModel];
    
}

#pragma mark --- UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        if (self.pageElementModel.pageEngine) {
            
            CSPageEngine *pageEngine = self.pageElementModel.pageEngine;
            
            [pageEngine removeElement:self.pageElementModel];
            
            [pageEngine refreshPage];
            
        }
    }
    
}

-(UIButton *)deleteButton{
    if (_deleteButton == nil) {
        _deleteButton = [[UIButton alloc]init];
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        _deleteButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _deleteButton.backgroundColor = COLOR_THEME;
        _deleteButton.layer.shadowColor = [UIColor blackColor].CGColor;
        _deleteButton.layer.shadowOffset = CGSizeMake(0, 1);
        _deleteButton.layer.shadowOpacity = 0.3;
        [self styleDevelopmentButton:_deleteButton];
        [_deleteButton addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

-(UIButton *)addButton{
    if (_addButton == nil) {
        _addButton = [[UIButton alloc]init];
        [_addButton setTitle:@"添加" forState:UIControlStateNormal];
        _addButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _addButton.backgroundColor = COLOR_THEME;
        _addButton.layer.shadowColor = [UIColor blackColor].CGColor;
        _addButton.layer.shadowOffset = CGSizeMake(0, 1);
        _addButton.layer.shadowOpacity = 0.3;
        [self styleDevelopmentButton:_addButton];
        [_addButton addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

-(UIButton *)editButton{
    if (_editButton == nil) {
        _editButton = [[UIButton alloc]init];
        [_editButton setTitle:@"设置" forState:UIControlStateNormal];
        _editButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _editButton.backgroundColor = COLOR_THEME;
        _editButton.layer.shadowColor = [UIColor blackColor].CGColor;
        _editButton.layer.shadowOffset = CGSizeMake(0, 1);
        _editButton.layer.shadowOpacity = 0.3;
        [self styleDevelopmentButton:_editButton];
        [_editButton addTarget:self action:@selector(editClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editButton;
}

-(void)styleDevelopmentButton:(UIButton *)targetButton{
    targetButton.backgroundColor = COLOR_THEME;
//    targetButton.layer.borderColor = COLOR_THEME.CGColor;
    [targetButton setTitleColor:COLOR_THEME forState:UIControlStateNormal];
    [targetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    targetButton.layer.borderWidth = 0;
    targetButton.layer.cornerRadius = 2;
    targetButton.layer.shadowOpacity = 0.0;
    _editButton.layer.shadowOffset = CGSizeMake(0, 0);
}




//-(UIButton *)editButton{
//    if (_editButton == nil) {
//        _editButton = [[UIButton alloc]init];
//    }
//    return _editButton;
//}

-(UIView *)backgroundView{
    if (_backgroundView == nil) {
        _backgroundView = [[UIView alloc]init];
        _backgroundView.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
        _backgroundView.layer.cornerRadius = 5;
        _backgroundView.layer.masksToBounds = YES;
    }
    return _backgroundView;
}


@end
